﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Draco.OWF.RT.Engine;
using System.IO;

namespace Draco.OWF.Shell
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("Draco.OWF [Version {0}]",Assembly.GetExecutingAssembly().GetName().Version));
            Console.WriteLine("Copyright (c) 2009 Draco.  All rights reserved.");

            Draco.OWF.RT.Engine.Shell shell = new Draco.OWF.RT.Engine.Shell();

            if (args != null && args.Length > 0)
            {
                String profile = args[0];
                String[] cmds = ReadCommandFile(profile);
                if (cmds != null)
                {
                    foreach(String cd in cmds)
                        shell.exec(cd);
                }
            }

            Console.Write(">");
            String cmd = Console.ReadLine();
            while (cmd != "exit")
            {
                shell.exec(cmd);
                Console.Write(">");
                cmd = Console.ReadLine();
            }
            shell.Dispose();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static String[] ReadCommandFile(String fileName)
        {
            if (String.IsNullOrWhiteSpace(fileName)) return null; ;
            if (!File.Exists(fileName))
            {
                Console.WriteLine(String.Format("ning:WarnFile [{0}] doesn't exist", fileName));
                return null;
            }
            String[] cmds = File.ReadAllLines(fileName, Encoding.UTF8);
            return cmds;
        }
    }
}
